<?php
require_once 'models/model_manager.php';
require_once 'models/bataille.php';
require_once 'models/camp_manager.php';

class bataille_manager extends Manager
{
	protected $_date;
	
	public function __construct($db) {
		parent::__construct($db);
		$this->_date = new date_historique_manager($this->_db);
	}	

	public function get_all_by_conflit($idconflit) {
		$batailles = array();
		$query = "SELECT * FROM bataille WHERE idConflit = ". $idconflit;
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		
		while ($data = oci_fetch_assoc($result)) {
			$bataille = new bataille($data); 
			$bataille->set_debut($this->get_date_debut($bataille->get_idbataille()));
			$bataille->set_fin($this->get_date_fin($bataille->get_idbataille()));
			$c = new camp_manager($this->_db);
			$bataille->set_camps($c->get_all_by_bataille($bataille->get_idbataille()));
			$batailles[] = $bataille;
		}
		return $batailles;
	}
	
	public function set_date_debut($batailles) {
		foreach ($batailles as $b) {
			$b->set_debut($this->get_date_debut($b->get_idbataille()));
		}
	}
	
	public function set_date_fin($batailles) {
		foreach ($batailles as $b) {
			$b->set_fin($this->get_date_fin($b->get_idbataille()));
		}
	}

	public function set_camps($batailles) {
		$c = new camp_manager($this->_db);
		foreach ($batailles as $b) {
			$b->set_camps($c->get_all_by_bataille($b->get_idbataille()));
		}
	}
	
	public function get_date_debut($id) {
		$query =
			"SELECT to_char(\"DATE\", 'DD/MM/SYYYY') FROM date_historique
			WHERE reference = 1
				AND idDate IN (SELECT idDate FROM debut_bataille
								WHERE idBataille = " . $id . ")";
		$result = oci_parse($this->_db, $query);
		
		oci_execute($result);
		$d = oci_fetch_row($result);
		return $d[0];
	}
	
	public function get_date_fin($id) {
		$query =
			"SELECT to_char(\"DATE\", 'DD/MM/SYYYY') FROM date_historique
			WHERE reference = 1
				AND idDate IN (SELECT idDate FROM fin_bataille
								WHERE idBataille = " . $id . ")";
		$result = oci_parse($this->_db, $query);
		
		oci_execute($result);
		$d = oci_fetch_row($result);
		return $d[0];
	}
	
	public function max_id() {
		$query = "SELECT max(idBataille) FROM bataille";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$r = oci_fetch_row($result);
		return $r[0];
	}

	public function add_or_update($e,$idconflit){
		$query = "SELECT COUNT(*) FROM bataille WHERE idBataille=".$e->get_idbataille();
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$d = oci_fetch_row($result);
		if($d[0] == 0)$this->add($e,$idconflit);
		else{
			$query = "UPDATE bataille SET nomBataille='".$e->get_nombataille()."',wikiBataille='".$e->get_wikibataille()."' WHERE idBataille=".$e->get_idbataille()."";
			$result = oci_parse($this->_db, $query);
			oci_execute($result);
			if($e->get_debut() != ""){
				$this->add_debut($e->get_idbataille(),$e->get_debut());
			}
			if($e->get_fin() != ""){
				$this->add_fin($e->get_idbataille(),$e->get_fin());
			}
			$r = new camp_manager($this->_db);
			foreach($e->get_camps() as $c){
				$r->add_or_update($c,$e->get_idbataille());
			}
		}
	}
	
	public function add($e,$idconflit) {
		$query = "INSERT INTO bataille(idBataille, idConflit, nomBataille, wikiBataille)
			VALUES (" . $e->get_idbataille() . "," . $idconflit . ",'" . $e->get_nombataille() . "','" . $e->get_wikibataille() . "')";
		$result = oci_parse($this->_db, $query);
		echo "<br>" . $query . "<br><br>";
		oci_execute($result);
		//echo "coucou".$e->get_debut();
		if($e->get_debut() != ""){
			$this->add_debut($e->get_idbataille(),$e->get_debut());
		}
		if($e->get_fin() != ""){
			$this->add_fin($e->get_idbataille(),$e->get_fin());
		}
		$r = new camp_manager($this->_db);
		foreach($e->get_camps() as $c){
			$r->add_or_update($c,$e->get_idbataille());
		}
	}
	
	public function delete_by_id($id){
		$query = "DELETE FROM bataille WHERE idBataille =" . $id;
		$result = oci_parse($this->_db, $query);
		echo "<br>" . $query . "<br><br>";
		oci_execute($result);
	}
	
	public function add_debut($idBataille, $date) {
		$id = $this->_date->max_id()+1;
		$this->_date->add(new date_historique(array(
								"idDate" => $id, 
								"reference" => 1,
								'date' => $date)));
		$query = "INSERT INTO debut_bataille(idDate, idBataille) VALUES(" . $id . ", " . $idBataille . ")";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
	}

	public function add_fin($idBataille, $date) {
		$id = $this->_date->max_id()+1;
		$this->_date->add(new date_historique(array(
								"idDate" => $id, 
								"reference" => 1,
								'date' => $date)));
		$query = "INSERT INTO fin_bataille(idDate, idBataille) VALUES(" . $id . ", " . $idBataille . ")";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
	}
}
?>
